import Icon from '@/components/icon'
import { addChannelsVal, delChannelsVal } from '@/store/actions/home'
import { Toast } from 'antd-mobile'
import classNames from 'classnames'
import { useState } from 'react'
import { useDispatch } from 'react-redux'
import { useSelector } from 'react-redux'
import styles from './index.module.scss'

/**
 * 频道管理组件
 * @param {Number} props.tabActiveIndex 用户选中的频道的索引
 * @param {Function} props.onClose 关闭频道管理抽屉时的回调函数
 * @param {Function} props.onChannelClick 当点击频道列表中的某个频道时的会带哦函数
 */
const Channels = ({
  tabActiveIndex,
  onClose,
  onChannelClick,
  index,
  onChange1,
}) => {
  const userChannels = useSelector((state) => state.home.channels)
  // 推荐频道
  const likeChannels = useSelector((state) => {
    const { channels, allChannels } = state.home
    return allChannels.filter((item) => {
      if (channels.findIndex((v) => v.id === item.id) === -1) {
        return true
      }
      return false
    })
  })
  // 点击span频道高亮
  const onClickChannle1 = (i) => {
    if (editing) return
    onChange1(i)
    onClose()
  }
  // 编辑进入状态
  const [editing, setEditing] = useState(false)
  const dispatch = useDispatch()
  // 删除频道
  const delChannels = async (item, i) => {
    // console.log(111, item)
    if (userChannels.length <= 4) {
      Toast.info('至少保留4个频道了啦')
      return
    }
    await dispatch(delChannelsVal(item))
    Toast.success('删除成功', 0.5)
    // 删除的时候，需要处理高亮
    // 高亮处理
    // 1. 如果删除的 i 和 index相等，默认让推荐 0 高亮
    // 2. 如果删除的 i 小于 index, 默认让 i - 1高亮
    // 3. 如果删除的i  大于 index  不用处理
    if (i === index) {
      onChange1(0)
    }
    if (i < index) {
      onChange1(index - 1)
    }
  }
  // 添加频道
  const addChannels = async (item) => {
    // 添加频道逻辑
    await dispatch(addChannelsVal(item))
    Toast.success('添加成功', 0.5)
  }
  return (
    <div className={styles.root}>
      {/* 顶部栏：带关闭按钮 */}
      <div className="channel-header">
        <Icon type="iconbtn_channel_close" onClick={onClose} />
      </div>

      {/* 频道列表 */}
      <div className="channel-content">
        {/* 当前已选择的频道列表 */}
        {/* 切换编辑和进入样式 */}
        <div className={classNames('channel-item', editing ? 'edit' : '')}>
          <div className="channel-item-header">
            <span className="channel-item-title">我的频道</span>
            <span className="channel-item-title-extra">
              {editing ? '点击删除频道' : '点击进入频道'}
            </span>
            <span
              onClick={() => setEditing(!editing)}
              className="channel-item-edit"
            >
              {editing ? '保存' : '编辑'}
            </span>
          </div>

          <div className="channel-list">
            {userChannels.map((item, i) => (
              <span
                onClick={() => onClickChannle1(i)}
                key={item.id}
                className={classNames(
                  'channel-list-item',
                  i === index ? 'selected' : ''
                )}
              >
                {item.name}
                <Icon
                  type="iconbtn_tag_close"
                  onClick={() => delChannels(item, i)}
                />
              </span>
            ))}
          </div>
        </div>

        {/* 推荐的频道列表 */}
        <div className="channel-item">
          <div className="channel-item-header">
            <span className="channel-item-title">频道推荐</span>
            <span className="channel-item-title-extra">点击添加频道</span>
          </div>
          <div className="channel-list">
            {likeChannels.map((item) => (
              <span
                onClick={() => addChannels(item)}
                key={item.id}
                className="channel-list-item"
              >
                + {item.name}
              </span>
            ))}
          </div>
        </div>
      </div>
    </div>
  )
}

export default Channels
